package com.adinnet.admin.classs.service.impl;


import com.adinnet.admin.child.mapper.ChildMapper;
import com.adinnet.admin.child.model.Child;
import com.adinnet.admin.classs.mapper.ClasssMapper;
import com.adinnet.admin.classs.model.Classs;
import com.adinnet.admin.classs.model.vo.ClassResp;
import com.adinnet.admin.classs.model.vo.ClasssReq;
import com.adinnet.admin.classs.service.ClasssService;
import com.adinnet.admin.order.model.vo.OrderResp;
import com.adinnet.admin.school.mapper.SchoolMapper;
import com.adinnet.admin.school.model.School;
import com.adinnet.admin.system.model.vo.SysUserInfo;
import com.adinnet.common.biz.enums.RoleCode;
import com.adinnet.admin.system.model.vo.SysUserInfo;
import com.adinnet.common.biz.enums.RoleCode;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 班级名称 服务实现类
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-02
 */
@Service
public class ClasssServiceImpl extends ServiceImpl<ClasssMapper, Classs> implements ClasssService {

    @Autowired
    private ClasssMapper classsMapper;

    @Autowired
    private ChildMapper childMapper;

    @Autowired
    private SchoolMapper schoolMapper;

    /**
     * 超级管理员查询所有班级
     * 机构查询所管辖校区下的班级
     * 学校查询本校的班级
     * 老师查询所在班级
     * @param sysUserInfo
     * @author zYshuai
     * @Date: 2020/12/11 15:39
    * @param classsReq:
    * @return: java.util.List<com.adinnet.admin.classs.model.vo.ClassResp>
     */
    @Override
    public PageResult<ClassResp> schoolList(ClasssReq classsReq, SysUserInfo sysUserInfo) {
        if ((Long) classsReq.getPage() == null || (Long) classsReq.getLimit() == null) {
            classsReq.setPage(1);
            
            classsReq.setLimit(10);
        }
        Page page = new Page<OrderResp>(classsReq.getPage(), classsReq.getLimit());
        List<ClassResp> classResps = Lists.newArrayList();
        if (RoleCode.ORGANIZATION.getCode().equals(sysUserInfo.getMaxRole())) {
            classsReq.setToOrganizationId(sysUserInfo.getOrganizationId());
            classResps = classsMapper.schoolList(page, classsReq.getClassName(), classsReq.getSchoolId(), classsReq.getToOrganizationId(),null);
        }
        if (RoleCode.SCHOOL.getCode().equals(sysUserInfo.getMaxRole())) {
            classsReq.setToOrganizationId(sysUserInfo.getOrganizationId());
            classsReq.setSchoolId(sysUserInfo.getSchoolId());
            classResps = classsMapper.schoolList(page, classsReq.getClassName(), classsReq.getSchoolId(), classsReq.getToOrganizationId(),null);
        }
        if (RoleCode.CLASSS.getCode().equals(sysUserInfo.getMaxRole())) {
            classsReq.setToOrganizationId(sysUserInfo.getOrganizationId());
            classsReq.setSchoolId(sysUserInfo.getSchoolId());
            classResps = classsMapper.schoolList(page, classsReq.getClassName(), classsReq.getSchoolId(), classsReq.getToOrganizationId(),sysUserInfo.getId());
        }
        if (RoleCode.ADMIN.getCode().equals(sysUserInfo.getMaxRole())) {
            classResps = classsMapper.schoolList(page, classsReq.getClassName(), classsReq.getSchoolId(), classsReq.getToOrganizationId(),null);

        }
        return new PageResult<ClassResp>(classResps, page.getTotal());
    }

    /**
     * @Description:  删除班级
     * @author zYshuai
     * @Date: 2020/12/11 16:15
    * @return: null
     */
    @Override
    public JsonResult delete(Integer classId) {
        Classs classs = classsMapper.selectById(classId);
        if (null == classs) {
            return JsonResult.error("查询班级失败");
        }
        List<Child> childList = childMapper.selectList(new QueryWrapper<Child>().eq("class_id", classId).eq("deleted", 0));
        if (CollectionUtils.isNotEmpty(childList)) {
            return JsonResult.error("班级内有" + childList.size() + "个学员，移除学员后才可删除班级。");
        }
        classs.setDeleted(true);
        classsMapper.updateById(classs);
        return JsonResult.ok("删除成功");
    }

    /**
     * 根据机构id查询所管辖的班级
     * @author chenjia
     * @param organizationId
     * @return
     */
    @Override
    public List<Classs> selectClasssByOrgId(Integer organizationId) {
        return classsMapper.selectClasssByOrgId(organizationId);
    }


    /**
     * 根据校区id查询学校列表
     * @param schoolId
     * @return
     */
    @Override
    public List<Classs> getListBySchoolId(Integer schoolId, SysUserInfo sysUserInfo) {
        QueryWrapper queryWrapper=new QueryWrapper();
        if(schoolId!=null && schoolId.intValue()>0){
            queryWrapper.eq("school_id",schoolId);
        }
        if(RoleCode.SCHOOL.getCode().equals(sysUserInfo.getMaxRole())){
            queryWrapper.eq("school_id",sysUserInfo.getSchoolId());
        }
        queryWrapper.eq("deleted",0);
        return baseMapper.selectList(queryWrapper);
    }

    /** * 超级管理员查询所有班级
     * 机构查询机构下的所有班级
     * 学校查询本校的班级
     * 老师查询所在学校的班级
     * @Description:
     * @author zYshuai
     * @Date: 2020/12/15 16:51
    * @param sysUserInfo:
    * @return: java.util.List<com.adinnet.admin.classs.model.Classs>
     */
    @Override
    public List<Classs> getListByUserRole(SysUserInfo sysUserInfo) {
        QueryWrapper<Classs> queryWrapper = new QueryWrapper();
        if (RoleCode.ORGANIZATION.getCode().equals(sysUserInfo.getMaxRole())) {
            List<School> schoolList = getListByOrganizationId(sysUserInfo.getOrganizationId(), sysUserInfo);
            if (CollectionUtils.isNotEmpty(schoolList)) {
                List<Integer> schoolIds = schoolList.stream().map(School::getId).collect(Collectors.toList());
                queryWrapper.in("school_id", schoolIds);
            }
        }
        if (RoleCode.SCHOOL.getCode().equals(sysUserInfo.getMaxRole())) {
            queryWrapper.eq("school_id", sysUserInfo.getSchoolId());
        }
        if (RoleCode.CLASSS.getCode().equals(sysUserInfo.getMaxRole())) {
            queryWrapper.eq("sys_user_id", sysUserInfo.getId());
        }
        queryWrapper.eq("deleted", 0);
        return classsMapper.selectList(queryWrapper);
    }


    /**
     * 根据导入的excel中的学校和班级名称和登陆人的权限查询班级
     * @author chenjia
     * @Date: 2020/12/16 16:31
     * @param schoolName
     * @param className
     * @return
     */
    @Override
    public Classs selectClasssForExcel(String schoolName, String className,Integer organizationId,Integer schoolId,Integer sysUserId) {
        return classsMapper.selectClasssForExcel(schoolName,className,organizationId,schoolId,sysUserId);
    }

    /**
     * 根据机构id查询学校列表
     * @param organizationId
     * @return
     */
    public List<School> getListByOrganizationId(Integer organizationId,SysUserInfo sysUserInfo) {
        QueryWrapper<School> queryWrapper=new QueryWrapper();
        if(organizationId!=null && organizationId.intValue()>0){
            queryWrapper.eq("organization_id",organizationId);
        }
        if(RoleCode.ORGANIZATION.getCode().equals(sysUserInfo.getMaxRole())){
            queryWrapper.eq("organization_id",sysUserInfo.getOrganizationId());
        }
        if(RoleCode.SCHOOL.getCode().equals(sysUserInfo.getMaxRole())){
            queryWrapper.eq("id",sysUserInfo.getSchoolId());
        }
        if(RoleCode.CLASSS.getCode().equals(sysUserInfo.getMaxRole())){
            queryWrapper.eq("id",sysUserInfo.getSchoolId());
        }
        queryWrapper.eq("deleted",0);
        return schoolMapper.selectList(queryWrapper);
    }

}
